<?php

/**
 *下载控制器
 *@author susy<susy.liang@ilongyuan.com.cn>
 */

namespace Home\Controller;
use Think\Controller;
use Api\Event\CacheEvent;

class DownloadController extends Controller {

	/**
	 *基础参数的初始化
	 */
	protected function _initialize() {
		/* 读取站点配置 */
		$config = api('Config/lists');
		C($config); //添加配置
		if (!C('WEB_SITE_CLOSE')) {
			$this->error('站点已经关闭，请稍后访问~');
		}
		define('IS_WEIXIN', is_weixin());
	}

    public function _empty()
    {
        $this->index();
    }

	// 更新日志
	public function log() {
		$logs = service('AppVersion')->where(['type' => 'pc', 'status' => 1])->select();
		$this->assign('logs', $logs);
		dump($logs);
	}

	/**
	 *下载页入口
	 */
	public function index() {
		C('DEFAULT_THEME', 'download');
		if (is_mobile_request()) {
			return $this->mobile();
		} else {
			$this->tab = 'pc';
			$this->logs = $this->update_log('pc');
			C('HTML_FILE_SUFFIX', '.html');
			$this->buildHtml('index', APP_PATH.'../download/', 'index', 'utf8');
		}
		$this->display('index');
	}

	public function app()
    {
    	C('DEFAULT_THEME', 'download');
    	$this->tab = 'app';
    	$this->logs = [
    		'ios' => $this->update_log('ios'),
    		'android' => $this->update_log('android')
    	];
    	C('HTML_FILE_SUFFIX', '.html');
		$this->buildHtml('index', APP_PATH.'../download/app/', 'app', 'utf8');
        $this->display('app');
    }

    public function mobile()
    {
    	if (!IS_MOBILE) {
    		return $this->app();
    	}
    	C('DEFAULT_THEME', 'download');
    	C('HTML_FILE_SUFFIX', '.html');
    	$this->buildHtml('index', APP_PATH.'../download/mobile/', 'mobile', 'utf8');
		return $this->display('mobile');
    }

	public function tutorial() {
		C('DEFAULT_THEME', 'ui3');
		$this->assign('type','tutorial');
		$this->assign('title','使用教程');
		$this->display('index');
	}

	public function android() {
		$androidRes = event('AppVersion')->getLatest('android');
		event('Statis')->operDownload(1, $androidRes['internal_version']);
		header("location: {$androidRes['url']}");
	}

	public function pc() {
		$platform = I('platform', '', 'trim');
		if ($platform == 'bigfoot') {
			$res = event('AppVersion')->getLatest('pc_bigfoot');
			if ($res) {
				header("location: {$res['url']}");
			}
			return;
		}
		$res = event('AppVersion')->getLatest('pc');
		event('Statis')->operDownload(5, $res['internal_version']);
		header("location: {$res['url']}");
	}

	public function ios() {
		$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
		$isios = strpos($agent, 'iphone') || strpos($agent, 'ipad');
		$this->browser = strpos($agent, 'micromessenger') ? 'weixin' : 'normal';
		if (IS_WEIXIN || strpos($agent, 'weibo') !== false) {
			C('DEFAULT_THEME', 'mobile3');
			return $this->display('weixin');
		} else {
			$res = event('AppVersion')->getLatest('ios');
			header("location: {$res['url']}");
		}
	}

	public function update_file() {
		$file = C('TOOL_UPDATE_EXE');

		$platform = I('platform', '', 'trim');
		if ($platform == 'bigfoot') {
			$file = C('TOOL_UPDATE_EXE_BIGFOOT');
		}

		if ($file) {
			header("location: {$file}");
		}
	}

    public function update_log($client_type)
    {
    	$Redis = CacheEvent::instance(0);
    	$key = "IMBA.CACHE.DOWNLOAD.VERSION_LOG.{$client_type}";
    	$data = $Redis->get($key);
    	$data = json_decode($data, true);
    	if (empty($data)) {
    		$data = [];
	        $list = event('AppVersion')->lists(['type' => $client_type], 'outer_version,create_time,detail,log,url');
	        foreach ($list as &$value) {
                $value['log'] = $value['log'] ? explode("\n", $value['log']) : [];
	            $year = date('Y', $value['create_time']);
	            $value['year'] = $year;
	            $value['public_date'] = date('m-d', $value['create_time']);
	            $data[$year][] = $value;
	        }
	        $Redis->setEx($key, 600, json_encode($data));
    	}
        unset($Redis);
        return $data;
    }
}